<?php
namespace App\Controller;

use App\Common\Controller\AbstractController;
use App\Exception\AppException;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;

class UploadController extends AbstractController{

    /**
     * @Inject()
     * @var Filesystem
     */
    private $fileSystem;

    public function index(){
        $file = $this->request->file("file");
        if(!$file){
            throw new AppException("文件不能为空");
        }
        $filename = md5_file($file->getRealPath()) . "." .$file->getExtension();
        $this->fileSystem->put($filename,file_get_contents($file->getRealPath()));
        return $this->response->json($this->fileSystem->getAdapter()->getUrl($filename));
    }
}